package cn.jiahua.hospital.service.impl;

import cn.jiahua.hospital.mapper.NewsMapper;
import cn.jiahua.hospital.pojo.News;
import cn.jiahua.hospital.service.NewsService;
import cn.jiahua.hospital.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;

import cn.jiahua.hospital.dto.Page;
import cn.jiahua.hospital.constants.Constants;

import javax.annotation.Resource;

@Service
public class NewsServiceImpl implements NewsService {


    @Autowired
    RedisUtil redisUtil;


    @Resource
    private NewsMapper newsMapper;

    @Override
    public News getNewsById(Integer newsId) {
        return newsMapper.getNewsById(newsId);
    }

    @Override
    public List<News> getNewsListByMap(Map<String, Object> param) {
        return newsMapper.getNewsListByMap(param);
    }

    @Override
    public Integer getNewsCountByMap(Map<String, Object> param) {
        return newsMapper.getNewsCountByMap(param);
    }

    @Override
    public Integer qdtxAddNews(News news) {
        news.setNewsCreateTime(new Date());
        return newsMapper.insertNews(news);
    }

    @Override
    public Integer qdtxModifyNews(News news) {
        news.setNewsModifyTime(new Date());
        return newsMapper.updateNews(news);
    }

    @Override
    public Integer qdtxDeleteNewsById(Integer newsId) {
        return newsMapper.deleteNewsById(newsId);
    }

    @Override
    public Integer qdtxBatchDeleteNews(String ids) {
        Map<String, List<String>> param = new HashMap<String, List<String>>();
        String[] paramArrays = ids.split(",");
        List<String> idList = new ArrayList<String>();
        for (String temp : paramArrays) {
            idList.add(temp);
        }
        param.put("ids", idList);
        return newsMapper.batchDeleteNews(param);
    }

    @Override
    public Page<News> queryNewsPageByMap(Map<String, Object> param, Integer pageNo, Integer pageSize) {

        if(!redisUtil.hasKey("page")){

            Integer total = newsMapper.getNewsCountByMap(param);
            pageNo = pageNo == null ? Constants.DEFAULT_PAGE_NO : pageNo;
            pageSize = pageSize == null ? Constants.DEFAULT_PAGE_SIZE : pageSize;
            Page page = new Page(pageNo, pageSize, total);
            param.put("beginPos", page.getBeginPos());
            param.put("pageSize", page.getPageSize());
            List<News> newsList = newsMapper.getNewsListByMap(param);
            page.setList(newsList);
            redisUtil.set("news",page,60*60*24);
            return page;

        }
        return (Page<News>) redisUtil.get("page");

    }

    @Override
    public List<News> newsSelectPage(String newsTitle, String newsAuthor, Integer pageNo, Integer pageSize) {

        if(!redisUtil.hasKey("news")){
          List<News> newsList=  newsMapper.newsSelectPage(newsTitle, newsAuthor, pageNo, pageSize);
          redisUtil.set("news",newsList,60*60*24);
          return newsList;
        }
        return (List<News>) redisUtil.get("news");
    }

    @Override
    public int newsSelectCount(String newsTitle, String newsAuthor) {

        if(!redisUtil.hasKey("newsCount")){
            Integer newsCount=  newsMapper.newsSelectCount(newsTitle, newsAuthor);
            //redisUtil.set("news",newsCount,60*60*24);
            return newsCount;
        }
        return (int) redisUtil.get("newsCount");


    }
}